package chenjiuxu.seniorui.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

import chenjiuxu.seniorui.R;

/**
 * Created by 15705 on 2018/3/5.
 */
public class BreakpointCircularView extends View {
    private final int lineColorBackground;//进度条背景
    private int lineColor;//进度颜色
    private float lineSize;//进度条宽
    private float maxTime = 10000;//最大时间
    private int thisTime = 0;//当期时间
    private final Paint paint;
    private Float value = 1f;
    private ValueAnimator animator;
    private float Percentage;
    private Timer timer;
    private MyTimerTask myTimerTask;
    private OnEndListener listener;


    public BreakpointCircularView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BreakpointCircularView);
        lineSize = array.getDimension(R.styleable.BreakpointCircularView_bcv_lineSize, 20);
        lineColor = array.getColor(R.styleable.BreakpointCircularView_bcv_lineColor, Color.YELLOW);
        lineColorBackground = array.getColor(R.styleable.BreakpointCircularView_bcv_lineColorBackground, Color.WHITE);
        maxTime = array.getInt(R.styleable.BreakpointCircularView_bcv_max, 10000);
        array.recycle();
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2;
        //画背景圆环
        float radius = (center - lineSize / 2) / 1.5f * value;
        paint.setColor(lineColorBackground);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(lineSize); // 圆环的宽度
        paint.setAntiAlias(true);//抗锯齿
        canvas.drawCircle(center, center, radius, paint);//空心圆
        //红色圆圈
        paint.setColor(lineColorBackground);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(0); // 圆环的宽度
        paint.setAntiAlias(true);//抗锯齿
        paint.setColor(Color.RED);
        canvas.drawCircle(center, center, (center - lineSize / 2) / 1.8f, paint);//空心圆
        //画圆弧 进度
        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
        paint.setColor(lineColor);
        paint.setStrokeWidth(lineSize);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);//圆形线冒
        canvas.drawArc(oval, -180, 360 * Percentage, false, paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startTimer(true);
                startAnimator(true);
                break;
            case MotionEvent.ACTION_UP:
                startTimer(false);
                startAnimator(false);
                break;
        }
        return true;
    }


    //开始动画
    private void startAnimator(boolean isDown) {
        if (animator == null) {
            animator = ValueAnimator.ofFloat(1f, 1.5f);
            animator.setDuration(150);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    value = (Float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
        }
        if (isDown) {
            animator.start();
        } else {
            animator.reverse();
        }

    }

    //开始计时
    private void startTimer(boolean isDown) {
        if (thisTime > maxTime) return;
        if (timer == null) timer = new Timer();
        if (isDown) {
            myTimerTask = new MyTimerTask();
            if (listener != null) listener.startListener();
            timer.schedule(myTimerTask, 0, 42);
        } else {
            myTimerTask.cancel();
            timer.purge();
            myTimerTask = null;
            if (listener != null) listener.stageListener();
        }
    }

    //计时完成
    public void setOnEndListener(OnEndListener listener) {
        this.listener = listener;
    }

    public interface OnEndListener {
        void endListener();

        void startListener();

        void stageListener();
    }

    //计时任务
    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            thisTime += 42;
            if (thisTime > maxTime) {
                myTimerTask.cancel();
                timer.purge();
                if (listener != null) listener.stageListener();
                if (listener != null) listener.endListener();
                return;
            }
            Percentage = thisTime / maxTime;
            postInvalidate();

        }
    }

    public float getMaxTime() {
        return maxTime;
    }

    public void setMaxTime(float maxTime) {
        this.maxTime = maxTime;
    }

    public int getThisTime() {
        return thisTime;
    }

    public void setThisTime(int thisTime) {
        this.thisTime = thisTime;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (timer != null) timer.cancel();
    }
}
